//
//  MainCommentController.m
//  soccer
//
//  Created by 上官千城 on 15/9/11.
//  Copyright (c) 2015年 上官千城. All rights reserved.
//

#import "OMainCommentController.h"

@interface OMainCommentController ()

@end

@implementation OMainCommentController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    //数据请求
    [self afnGetRequest];
    
}
//tableView协议方法
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"MainCommentCell";
    OMainCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[OMainCommentCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        
    }
    OMainComment *mainComment = [self.moderArray objectAtIndex:indexPath.row];
    cell.mainComment = mainComment;
    return cell;
}
//行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 110;
}
//cell个数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.moderArray.count;
}

//点击Cell方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"点击事件");
}

-(void)afnGetRequest
{
    
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    NSString *url_string = @"http://api.m.mtime.cn/Movie/Video.api?movieId=218897";
    
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"application/x-javascript",@"text/html",nil];
    
    
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
//        NSLog(@"使用AFN进行get请求 ===  %@",responseObject);
        //        [[DateHandel shareHandle]dropTable];
        //        [[DateHandel shareHandle]createTable];
        //        [self.array removeAllObjects];
        
        NSMutableDictionary *allDic = responseObject;
        NSMutableArray *array = [NSMutableArray array];
        array = [allDic objectForKey:@"videoList"];
            self.moderArray = [NSMutableArray array];
            for (NSMutableDictionary *dic in array) {
                OMainComment *movieDet = [[OMainComment alloc] init];
                movieDet.movieImageView = [dic objectForKey:@"image"];
                movieDet.nameLabel = [dic objectForKey:@"title"];
                movieDet.length = [[dic objectForKey:@"length"]integerValue];
                movieDet.url = [dic objectForKey:@"url"];
                [self.moderArray addObject:movieDet];
            
        }

        
        [self.tableView reloadData];
        //        [self.HUD hide:YES];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败==== %@",error);
        //        [self.HUD hide:YES];
        
        
    }];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
